// user_announcement.js

import express from 'express';
const router = express.Router();
import { createConnectionPool } from '../../mysql/index.js';

const connection = await createConnectionPool();

router.get('/GatewayAddress/get', async (req, res) => {
    try {
        // 执行查询
        const query = 'SELECT id, GatewayAddress, IpAddress,ParseId FROM mygateway_address';
        const result = await connection.query(query);
        // 将结果发送给客户端
        res.status(200).send({ code: 200, message: '获取数据成功', data: result[0] })
    } catch (error) {
        console.error('Error fetching payment records:', error);
        res.status(500).json({ error: 'Failed to fetch payment records' });
    }
})

// 生成订单号的函数

router.post('/GatewayAddress/add', async (req, res) => {
    if (!req.body.GatewayAddress || !req.body.IpAddress || !req.body.ParseId) {
        res.status(400).json({ error: 'businessUser_id, businessUser_accountNumber, money, and order_state are required parameters' });
        return
    }

    const { GatewayAddress, IpAddress, ParseId } = req.body;


    try {

        const sql = `INSERT INTO mygateway_address (GatewayAddress, IpAddress, ParseId) 
                VALUES (?, ?, ?)`;

        // 执行 SQL 查询
        await connection.query(sql, [GatewayAddress, IpAddress, ParseId]);

        console.log('Payment record added successfully');
        res.status(200).json({ code: 200, message: 'Payment record added successfully' });
    } catch (error) {
        console.error('Error executing query: ' + error.stack);
        res.status(500).json({ error: 'An error occurred while adding payment record' });
    }

})



router.delete('/GatewayAddress/delete', async (req, res) => {
    const { id } = req.body;

    try {
        // 检查参数是否提供
        if (!id) {
            return res.status(400).json({ error: 'businessUser_id is required' });
        }
        // 构造 SQL 查询
        const sql = 'DELETE FROM mygateway_address WHERE id = ?';
        // 执行 SQL 查询
        await connection.query(sql, [id]);

        console.log('Record with businessUser_id ' + id + ' deleted successfully');
        res.status(200).json({ code: 200, message: 'Record deleted successfully' });
    } catch (error) {
        console.error('Error deleting record:', error);
        res.status(500).json({ error: 'An error occurred while deleting record' });
    }
});


export default router;
